$(document).ready(function () {

    $('#pop-wrapper select').select0();

    $('#pop-wrapper .x-4-move').dragging({
        move: 'both',
        randomPosition: false,
        handler: '.panel-heading'
    });

    $('#pop-wrapper input.checkAll').off('click').on('click', function () {
        $('#pop-wrapper .attrContent input:checkbox').prop("checked", $(this).prop("checked"));
    });

    /*根据一级类别查询二级类别*/
    $('#pop-wrapper select[name="levelOne"]').off('change').on('change', function () {
        var id = $(this).val();
        var _html = '<option value="">-请选择-</option>';
        if (!id) {
            $('#pop-wrapper select[name="levelTwo"]').html(_html);
            $('#pop-wrapper select[name="levelTwo"]').cLearSelect0();
            $('#pop-wrapper select').select0();
            return;
        }
        ajaxSubmitResponseJSON({
            url: 'category/ajaxLevelTwoListByPid.htm/' + id,
            success: function (data) {
                if (data.status == 0) {
                    var o = data.data;
                    if (o && o.length > 0) {
                        for (var i = 0; i < o.length; i++) {
                            _html += '<option value="' + o[i].id + '">' + o[i].name + '</option>';
                        }
                    }
                    $('#pop-wrapper select[name="levelTwo"]').html(_html);
                    $('#pop-wrapper select').select0();
                }
            }
        });
    });

    $('#pop-wrapper .btn-4-save').off('click').on('click', function () {
        var id = $('#pop-wrapper input:hidden[name="id"]').val();
        var url = 'categoryParam/add.htm';
        if (id) {
            url = 'categoryParam/modify.htm';
        }

        var levelOne = $('#pop-wrapper [name="levelOne"]').val();
        if (!levelOne) {
            tipData({message: '请选择一级类别名称', status: -1});
            return;
        }

        var levelTwo = $('#pop-wrapper [name="levelTwo"]').val();
        /*if (!levelTwo) {
            tipData({message: '请选择二级类别名称', status: -1});
            return;
        }*/

        var levelId = ($.trim(levelTwo).length > 0) ? ($.trim(levelTwo)) : ($.trim(levelOne).length > 0 ? $.trim(levelOne) : '0');
        $('#pop-wrapper input:hidden[name="categoryId"]').val(levelId);

        var level = '0';
        if ($.trim(levelOne).length > 0) level = '1';
        if ($.trim(levelTwo).length > 0) level = '2';
        $('#pop-wrapper input:hidden[name="level"]').val(level);

        var params = [];
        var _ps = [];
        $('#pop-wrapper .attrContent').find('input[name="val"]:checked').each(function(){
            if($.trim($(this).val()).length > 0){
                _ps.push($.trim($(this).val()));
            }
        });
        if(_ps.length > 0){
            params.push({"params":_ps});
            $('#pop-wrapper input:hidden[name="paramData"]').val(JSON.stringify(params));
        } else {
            tipData({message: '请选择关联属性', status: -1});
            return;
        }

        ajaxSubmitResponseJSON({
            url: url,
            method: 'POST',
            data: $('#pop-wrapper form').serialize(),
            success: function (data) {
                if (data.status == 0) {
                    closeLayer();
                    $('#data-wrapper .btn-4-query').trigger('click');
                }
            }
        });
    });

});